package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.linlinjava.litemall.db.dao.LitemallBillMapper;
import org.linlinjava.litemall.db.domain.LitemallBill;
import org.linlinjava.litemall.db.domain.LitemallBillExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * @author: weilai
 * @date: 2019/12/7
 * @description:
 */
@Service
public class LitemallBillService {
    @Resource
    private LitemallBillMapper billMapper;

    /**
     * 查询all： 本商户 billList 分页带状态
     * @param brandName
     * @param status
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */

    public PageInfo<LitemallBill> query(String brandName, Short status, LocalDate startTime, LocalDate endTime, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        LitemallBillExample example = new LitemallBillExample();
        LitemallBillExample.Criteria criteria = example.createCriteria();
        if(null!=brandName){
            criteria.andBrandNameLike("%"+brandName+"%").andDeletedEqualTo(false);
        }
        if (status != null && status > 0) {
            criteria.andStatusEqualTo(status).andDeletedEqualTo(false);
        }
        if(null!=startTime&&null!=endTime){
            criteria.andAddTimeBetween(startTime,endTime);
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        return new PageInfo<>(billMapper.selectByExample(example));
    }

    /**
     * 主键
     * @param id
     * @return
     */
    public LitemallBill findById(Integer id) {
        return billMapper.selectByPrimaryKey(id);
    }

    /**
     * 创建一个BILL
     * @param bill
     * @return
     */
    public int create(LitemallBill bill){
        return billMapper.insertSelective(bill);
    }

    /**
     * 更新一个结算单
     * @param bill
     * @return
     */
    public int updateBill(LitemallBill bill){
        return billMapper.updateByPrimaryKeySelective(bill);
    }

    /**
     * 根据订单ID查找
     * @param id
     * @return
     */
    public LitemallBill findByOrder(Integer id) {
        LitemallBillExample example = new LitemallBillExample();
        example.or().andOrderIdEqualTo(id).andDeletedEqualTo(false);
        return billMapper.selectOneByExample(example);
    }

    /**
     * 删除结算单
     * @param id
     */
    public void delBill(Integer id){
        billMapper.deleteByPrimaryKey(id);
    }

}
